package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.PresentManagerTask;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.model.TodayData;
import com.mofang.powerdekorhelper.model.UserManagerTask;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.WorkPlantView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/9/3 0003.
 * 说明：
 */

public class WorkPlanPersenter extends BasePresent<WorkPlantView> {

    RetrofitSerives retrofitSerives;

    Call<ResultMessage> customerReportCall;
    Call<UserManagerTask> userManagerTaskCall;
    Call<PresentManagerTask> presentManagerTaskCall;
    Call<TodayData> todayDataCall;

    public void getCustomerReport(Integer userId, String customerName, String customerPhone) {

        JSONObject json = new JSONObject();
        try {
            json.put("usermanager_id", userId);
            json.put("customer_name", customerName);
            json.put("phone", customerPhone);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String jsonParams = json.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        customerReportCall = retrofitSerives.getCustomerReport(body);

        customerReportCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setCustomerReport(response.body());
                    } else {
                        view.onError(Constants.REPORT_ERROR);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getUserManagerTask(String jsonParams) {

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
//{"page":1,"pageSize":10,"param":{"user_manager_id":24}}  {"page":1,"pageSize":10,"param":{"user_manager_id":24,"state":2}}
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        userManagerTaskCall = retrofitSerives.getUserManagerTask(body);

        userManagerTaskCall.enqueue(new Callback<UserManagerTask>() {
            @Override
            public void onResponse(Call<UserManagerTask> call, Response<UserManagerTask> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setUserManagerTask(response.body());
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<UserManagerTask> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getPresentManagerTask(String jsonParams) {

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        presentManagerTaskCall = retrofitSerives.getPresentUerManagerTask(body);

        presentManagerTaskCall.enqueue(new Callback<PresentManagerTask>() {
            @Override
            public void onResponse(Call<PresentManagerTask> call, Response<PresentManagerTask> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setPresentUerManagerTask(response.body());
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<PresentManagerTask> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }


    public void getTodayData(Integer userId) {

        String jsonParams = "";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("sellerId", userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        todayDataCall = retrofitSerives.getTodayData(body);

        todayDataCall.enqueue(new Callback<TodayData>() {
            @Override
            public void onResponse(Call<TodayData> call, Response<TodayData> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setTodayData(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<TodayData> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {
    }
}
